#!/bin/sh

# Utility script to clean up after a MacVim build.

if [[ $# == 0 ]]; then
    echo "Usage: cleanup-after-build <MacVim_app> <remove_sparkle>"
    exit -1
fi

set -e

macvim_path=$1
remove_sparkle=$2

if [ "$remove_sparkle" == "1" ]; then
    sparkle_path="$macvim_path/Contents/Frameworks/Sparkle.framework"
    if [ -d "$sparkle_path" ]; then
        # Remove the entire Sparkle folder. Used when --disable-sparkle was set.
        # Using a clean up script is easier because there isn't an easy way to tell
        # Xcode not to link/copy it unless we make another target, or dynamically
        # patch the project file.
        (set -x
        rm -rf "$sparkle_path")
    fi
else
    sparkle_xpcservices_symlink="$macvim_path/Contents/Frameworks/Sparkle.framework/XPCServices"
    sparkle_xpcservices="$macvim_path/Contents/Frameworks/Sparkle.framework/Versions/Current/XPCServices"

    if [ -d "$sparkle_xpcservices" ]; then
        # This only happens when building using Sparkle 2. It contains XPC Services
        # files which are only necessary for sandboxed apps, and not recommended
        # otherwise. See https://sparkle-project.org/documentation/sandboxing/.
        (set -x
        rm -rf "$sparkle_xpcservices"
        rm "$sparkle_xpcservices_symlink")
    fi
fi
